<?php

class NewsCategory extends WP_Widget {

    function NewsCategory() {
        parent::WP_Widget(false, $name = 'Tinf5 - News Lastest Block of category');
    }

    function widget($args, $instance) {
        $title = $instance['title'];
        $number = $instance['number'];
        $cat = $instance['cat'];
        $type = $instance['type'];
        $block = $instance['block'];
        $count = count(allPostToday($cat));
        ?>
        <div class="block-new <?php echo $block; ?> cat-<?php echo $cat; ?>">
            <input class="cat-<?php echo $cat; ?>" name="cat[<?php echo $cat; ?>]" value="<?php echo $number.'-'.$type; ?>" type="hidden"/>
            <div class="row">
                <p class="count count-three fl" title="Có <?php echo $count; ?> tin trong ngày hôm nay"><?php echo $count; ?></p>
                <h3 class="fl"><?php echo $title; ?></h3>
                <span class="glyphicon glyphicon-chevron-down fr btn-shcate"></span>
            </div>
            <div class="row contentblock">
                <?php
                add_filter('posts_where', 'filter_where');
                query_posts(array('posts_per_page' => $number, 'cat' => $cat));
                while (have_posts()) : the_post();
                    echo getPostItem($type);
                endwhile;
                ?>
            </div>
        </div>
        <?php
        wp_reset_query();
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = ($new_instance['number']) ? $new_instance['number'] : 20;
        $instance['cat'] = $new_instance['cat'];
        $instance['type'] = $new_instance['type'];
        $instance['block'] = $new_instance['block'];
        return $instance;
    }

    function form($instance) {
        $title = isset($instance['title']) ? $instance['title'] : '';
        $number = isset($instance['number']) ? $instance['number'] : '';
        $cat = isset($instance['cat']) ? $instance['cat'] : '';
        $type = isset($instance['type']) ? $instance['type'] : '';
        $block = isset($instance['block']) ? $instance['block'] : '';

        $cats = get_terms('category', array('hide_empty' => false));
        $types = array(
            array('key' => 1, 'value' => 'Title-Image'),
            array('key' => 2, 'value' => 'Title-Text'),
        );
        $blocks = array(
            array('key' => 'block-style-one', 'value' => 'block style one'),
            array('key' => 'block-style-two', 'value' => 'block style two'),
            array('key' => 'block-style-three', 'value' => 'block style three'),
        );
        ?>
        <fieldset class="ether-form">
            <div>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
            </div>
            <div>
                <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number dislay:'); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
            </div>
            <div>
                <label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Category dislay:'); ?></label> 
                <select class="widefat" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>">
                    <?php foreach ($cats as $item): ?>
                        <option value="<?php echo $item->term_taxonomy_id; ?>" <?php echo (($item->term_taxonomy_id == $cat) ? 'selected' : ''); ?>><?php echo $item->name; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Type dislay:'); ?></label> 
                <select class="widefat" id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>">
                    <?php foreach ($types as $item): ?>
                        <option value="<?php echo $item['key']; ?>" <?php echo (($item['key'] == $type) ? 'selected' : ''); ?>><?php echo $item['value']; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label for="<?php echo $this->get_field_id('bock'); ?>"><?php _e('Block type dislay:'); ?></label> 
                <select class="widefat" id="<?php echo $this->get_field_id('block'); ?>" name="<?php echo $this->get_field_name('block'); ?>">
                    <?php foreach ($blocks as $item): ?>
                        <option value="<?php echo $item['key']; ?>" <?php echo (($item['key'] == $block) ? 'selected' : ''); ?>><?php echo $item['value']; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </fieldset>
        <?php
    }

}

register_widget('NewsCategory');
?>